package com.kmproject.haifu.mvp.presenter;

import android.app.Application;

import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.RxUtils;
import com.kmproject.haifu.mvp.contract.HousePublishStorePublishInfoContract;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.HousePayWayBean;
import com.kmproject.haifu.mvp.model.entity.beans.HousePublishResultBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 08/10/2019 12:10
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class HousePublishStorePublishInfoPresenter extends BasePresenter<HousePublishStorePublishInfoContract.Model, HousePublishStorePublishInfoContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public HousePublishStorePublishInfoPresenter(HousePublishStorePublishInfoContract.Model model, HousePublishStorePublishInfoContract.View rootView) {
        super(model, rootView);
    }
    /**
     * 更新信息
     * @param mpas
     */
    public void updateHouseRent(Map<String,Object> mpas)
    {
        mModel.updateHouseRent(mpas).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                        if (AppUtil.isSuccess(resultBean))
                        {
                            mRootView.updateHouseRentSuccess();
                        }else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }
    /**
     * 发布
     * @param params
     */
    public void  addHouseRent(Map<String, Object> params) {
        mModel.addHouseRent(params).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                        if (AppUtil.isSuccess(resultBean))
                        {

                            mRootView.addHouseRentSuccess();
                        }else {
                            mRootView.showMessage(resultBean.getMessage());
                        }
                    }
                });
    }
    /**
     * 获取付款方式
     *
     * @param value
     * @param category
     */
    public void getHousePayWayBean(String value, String category, List<HousePayWayBean> housePayWayBeans) {
        if (housePayWayBeans != null && !housePayWayBeans.isEmpty()) {
            mRootView.getHousePayWayBeanSuccess(housePayWayBeans);
            return;
        }//dictList4Category

        mModel.getHousePayWayBean(value, category).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean<List<HousePayWayBean>>>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean<List<HousePayWayBean>> listResultBean) {
                        if (AppUtil.isSuccess(listResultBean)) {
                            mRootView.getHousePayWayBeanSuccess(listResultBean.getData());
                        } else {
                            mRootView.showMessage(listResultBean.getMessage());
                        }

                    }
                });
    }
    /**
     * 得到名字 字符串
     *
     * @param housePayWayBeans
     * @return
     */
    public List<String> getHousePayWay(List<HousePayWayBean> housePayWayBeans) {
        List<String> temName = new ArrayList<>();
        if (housePayWayBeans == null || housePayWayBeans.isEmpty()) {
            return temName;
        }
        for (HousePayWayBean housePayWayBean : housePayWayBeans) {
            temName.add(housePayWayBean.getName());
        }
        return temName;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
